﻿module Sustainalytics.News {

    import Common = Sustainalytics.Common;

    // NewsViewController controller
    export class NewsViewController {

        public news: NewsDto;
        private $routeParams: any;
        private accountId: string;
        private userId: string;

        //Ctor.
        constructor(
            private $rootScope: ng.IScope,
            private $scope: ng.IScope,
            private $q: ng.IQService,
            private $location: ng.ILocationService,
            //private $routeParams: ng.route.IRouteParamsService,
            private RoutingService: Routing.IRoutingService,
            private NewsService: NewsService,
            private notificationFactory: Common.INotificationFactory,
            private $sce: ng.ISCEService) {

            var ctx = this;

            if (ctx.$rootScope['userInfo'] == null) {
                ctx.$rootScope['userInfo'] = angular.fromJson(localStorage.getItem('sust_userInfo'));
            }
            ctx.accountId = ctx.$rootScope['userInfo'].accountId;
            ctx.userId = ctx.$rootScope['userInfo'].userId;

            ctx.$routeParams = ctx.RoutingService.getRouteParams();
            $scope['newsView'] = ctx;
            ctx.getNewsById();
        }

        public getNewsById(): ng.IPromise<NewsDto> {
            var ctx = this;
            ctx.$rootScope['loading'] = true;

            // get news ID
            var newsId: string = ctx.$routeParams['newsId'];

            // get news by id
            var promiseGet: ng.IPromise<NewsDto> = ctx.NewsService.getNewsById(newsId, ctx.accountId);

            //on success
            promiseGet
                .then(function (data) {
                    ctx.news = data;
                })
                ['finally'](function () {
                    ctx.$rootScope['loading'] = false;
                });
            return promiseGet;
        }

        public RenderHTML(htmlCode: string) {
            var ctx = this;
            return ctx.$sce.trustAsHtml(htmlCode);
        }
    }


     // register controller 
    angular.module('Sustainalytics.News').controller('NewsViewController',
        ['$rootScope', '$scope', '$q', '$location', 'RoutingService', 'NewsService', 'notificationFactory', '$sce', NewsViewController]);
}
